package com.atguigu.spring.beans.cycle;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * @author 夏焰波
 * @date 2020-08-20 1:15
 */
public class MyBeanPostProcessor implements BeanPostProcessor {

    /**
     * 在init-method方法执行之前被调用
     *
     * @param bean     bean实例本身
     * @param beanName IOC容器配置的bean的名字
     * @return 返回给用户的bean，可以修改原生的bean，甚至返回一个新的bean
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessBeforeInitialization" + bean + beanName);

        if ("car".equals(beanName)) {
            //...
        }

        //通常做检查后把原生的bean返回
        return bean;
    }

    //在init-method方法执行之后被调用
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessAfterInitialization" + bean + beanName);

        Car car = new Car();
        car.setBrand("Ford");
        //通常做检查后把原生的bean返回
        return car;
    }
}
